package com.homegoods.designer.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.homegoods.designer.R;
import com.kedll.library.adapter.MyArrayAdapter;
import com.kedll.library.contants.OnItemClickListener;
import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.Parse;

import java.util.Map;


/**
 * @author QiQi
 */
public class ItemNeedTouGaoAdapter extends MyArrayAdapter<Map<String, Object>> {

    /**
     * 数据相关
     */
    private int width;

    /**
     * 点击事件接口
     */
    private OnItemClickListener l;

    private Parse parse;

    public ItemNeedTouGaoAdapter(Context context) {
        super(context);
        parse = Parse.getInstance();
        width = (MyUtils.getScreenWidth(context) - MyUtils.dp2px(8f) * 5) / 4;
    }

    /**
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.designer_item_i_need_tougao, null);
        }
        // 图片显示控件
        ImageView mIvPhoto = ViewHolder.getView(convertView, R.id.iv_photo);
        // 删除按钮
        ImageView mIvDelPhoto = ViewHolder.getView(convertView, R.id.iv_del_photo);
        // 进度百分比
        TextView mTvProgress1 = ViewHolder.getView(convertView, R.id.tv_progress_1);
        // 进度条
        ProgressBar mPb1 = ViewHolder.getView(convertView, R.id.pb_1);
        // 进度条布局
        RelativeLayout mRlProgress = ViewHolder.getView(convertView, R.id.rl_progress);
        // 图片、删除父布局
        RelativeLayout mRlPhoto = ViewHolder.getView(convertView, R.id.rl_photo);
        // 添加按钮
        ImageView mIvAddPhoto = ViewHolder.getView(convertView, R.id.iv_add_photo);
        // 父布局
        FrameLayout mFlPhoto = ViewHolder.getView(convertView, R.id.fl_photo);

        LayoutParams lp = mFlPhoto.getLayoutParams();
        lp.width = width;
        lp.height = width;
        mFlPhoto.setLayoutParams(lp);

        Map<String, Object> item = getItem(position);

        if (parse.parseBool(item.get("isAdd"))) {
            mRlPhoto.setVisibility(View.GONE);
            mIvAddPhoto.setVisibility(View.VISIBLE);
            mIvAddPhoto.setImageResource(R.drawable.add_img_img);
        } else {
            mRlPhoto.setVisibility(View.VISIBLE);
            mIvAddPhoto.setVisibility(View.GONE);
            String imgPath = parse.isNull(item.get("imagePath"));
            if (imgPath.startsWith("http://")) {
                BindingUtil.loadImageNoTransform(mIvPhoto, MyUtils.getMinImage(imgPath), null, null);
            } else {

                BindingUtil.loadImageNoTransform(mIvPhoto, "file://" + imgPath, null, null);
            }
        }
        // 是否提交中
        if (parse.parseBool(item.get("isSubmit"))) {
            mRlProgress.setVisibility(View.VISIBLE);
            mTvProgress1.setText(String.format("%d  %%", parse.parseInt(item.get("progress"))));
            mPb1.setProgress(parse.parseInt(item.get("progress")));
        } else {
            mRlProgress.setVisibility(View.GONE);
        }
        mIvPhoto.setOnClickListener(new MyOnClickListener(item, position, l));
        mIvDelPhoto.setOnClickListener(new MyOnClickListener(item, position, l));
        mIvAddPhoto.setOnClickListener(new MyOnClickListener(item, position, l));
        return convertView;
    }

    /**
     * 设置条目点击事件
     *
     * @param l
     */
    public void setOnItemClickListener(OnItemClickListener l) {
        this.l = l;
    }

    private static class MyOnClickListener implements OnClickListener {

        private Map<String, Object> item;
        private int position;
        private OnItemClickListener l;

        MyOnClickListener(Map<String, Object> item, int position, OnItemClickListener l) {
            this.item = item;
            this.position = position;
            this.l = l;
        }

        @Override
        public void onClick(View v) {
            if (l != null) {
                l.itemClick(v, item, position);
            }
        }

    }

}
